package com.ruoyi.rms.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.rms.domain.MemberMessage;
import com.ruoyi.rms.domain.OrderMessage;
import com.ruoyi.rms.enums.OrderStatus;
import com.ruoyi.rms.service.IMemberMessageService;
import com.ruoyi.rms.service.IOrderMessageService;
import com.ruoyi.rms.utils.IdUtils;
import com.ruoyi.rms.utils.RetUtils;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.rms.mapper.AssessMessageMapper;
import com.ruoyi.rms.domain.AssessMessage;
import com.ruoyi.rms.service.IAssessMessageService;

/**
 * 评价信息Service业务层处理
 * 
 * @author wyl
 * @date 2024-03-11
 */
@Service
@Slf4j
public class AssessMessageServiceImpl implements IAssessMessageService 
{
    @Autowired
    private AssessMessageMapper assessMessageMapper;

    @Autowired
    private IOrderMessageService orderMessageService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IMemberMessageService memberMessageService;

    /**
     * 查询评价信息
     * 
     * @param assessId 评价信息主键
     * @return 评价信息
     */
    @Override
    public AssessMessage selectAssessMessageByAssessId(Long assessId)
    {
        return assessMessageMapper.selectAssessMessageByAssessId(assessId);
    }

    /**
     * 查询评价信息列表
     * 
     * @param assessMessage 评价信息
     * @return 评价信息
     */
    @Override
    public List<AssessMessage> selectAssessMessageList(AssessMessage assessMessage)
    {
        return selectAssessWithNickName(assessMessageMapper.selectAssessMessageList(assessMessage));
    }

    /**
     * 联查评论者昵称
     * @param assessMessageList
     * @return
     */
    private List<AssessMessage> selectAssessWithNickName(List<AssessMessage> assessMessageList) {
        for (AssessMessage assess : assessMessageList) {
            String createBy = assess.getCreateBy();
            SysUser sysUser = sysUserService.selectUserByUserName(createBy);
            if (sysUser == null) {
                // 不是餐厅管理员发布的评论，那就是客户发的评论
                MemberMessage memberMessage = memberMessageService.getInfoByWxId(createBy);
                if (memberMessage == null) {
                    // 这是一个还未注册会员的客户发布的
                    assess.setNickName("微信用户" + createBy); // 微信用户+openid
                } else {
                    // 这是已经注册的会员发布的
                    assess.setNickName(memberMessage.getMemberName());
                }
            } else {
                // 这是餐厅管理员发布的
                assess.setNickName(sysUser.getNickName());
            }
        }

        return assessMessageList;
    }

    /**
     * 根据订单查询评论
     * @param orderId
     * @return
     */
    @Override
    public List<AssessMessage> selectAssessByOrderId(Long orderId) {
        return selectAssessWithNickName(assessMessageMapper.selectAssessByOrderId(orderId));
    }

    /**
     * 新增评价信息
     * 
     * @param assessMessage 评价信息
     * @return 结果
     */
    @Override
    public int insertAssessMessage(AssessMessage assessMessage)
    {
        Long orderId = assessMessage.getOrderId();
        OrderMessage orderMessage = orderMessageService.selectOrderMessageByOrderId(orderId);
        String status = orderMessage.getStatus();
        if (!OrderStatus.PAYED.getStatus().equals(status)) {
            log.error("此订单未付款，不能评价");
            return RetUtils.ERROR;
        }

        assessMessage.setAssessId(IdUtils.createId());
        assessMessage.setCreateTime(DateUtils.getNowDate());
        if (assessMessage.getCreateBy() == null) {
            // 如果评价人为空，则此时是网页端插入的评价信息，需要手动设置
            assessMessage.setCreateBy(SecurityUtils.getUsername());
        }
        return assessMessageMapper.insertAssessMessage(assessMessage);
    }

    /**
     * 修改评价信息
     * 
     * @param assessMessage 评价信息
     * @return 结果
     */
    @Override
    public int updateAssessMessage(AssessMessage assessMessage)
    {
        return assessMessageMapper.updateAssessMessage(assessMessage);
    }

    /**
     * 批量删除评价信息
     * 
     * @param assessIds 需要删除的评价信息主键
     * @return 结果
     */
    @Override
    public int deleteAssessMessageByAssessIds(Long[] assessIds)
    {
        return assessMessageMapper.deleteAssessMessageByAssessIds(assessIds);
    }

    /**
     * 删除评价信息信息
     * 
     * @param assessId 评价信息主键
     * @return 结果
     */
    @Override
    public int deleteAssessMessageByAssessId(Long assessId)
    {
        return assessMessageMapper.deleteAssessMessageByAssessId(assessId);
    }
}
